(function() {
    'use strict';
    angular
    	.module('app')
		.directive('documentClick', ['$document', function ($document) {
			return {
				restrict: 'A',
				scope: {
					state: '=documentState'
				},
				link: function (scope, element, attrs) {
					$document.bind('click', function (event) {
						if (scope.state && !element[0].contains(event.target)) {
							scope.$apply(function () {
								scope.state = false;
							});
						}
					});
				}
			}
		}])
		.directive('menuNav', menuNav)
		.controller('ctrl.myShortCut',['$scope','$timeout','$uibModalInstance','param','config','http','myUtils',myShortCut])
    	.controller('homeCtrl', homeCtrl);
		myShortCut.$inject=['$scope','$timeout','$uibModalInstance','param','config','http','myUtils'];
    	homeCtrl.$inject = ['$scope','$timeout','http','config','myUtils','$uibModal'];
      	function homeCtrl($scope,$timeout,http,config,myUtils,$uibModal) {
      		var vm = $scope.vm = {};
      		angular.extend( vm,{
				type : [],
				live_unusual : [],
				disk_space : [],
				disk_data : [
					{
						percent : 0,
						lineWidth: 5,
						trackColor: 'rgba(0,0,0,0.05)',
						barColor: '#fec400',
						scaleColor: 'transparent',
						size: 80,
						scaleLength: 0,
						lineCap: 'butt',
						animate:{
							duration:0,
							enabled:false
						}
					}
				],
      			init : function(){
					vm.disk_space.num = 0;
					vm.menuData=[
						{
							src:"http://mxu-img.dev.hoge.cn/material/auth/img/64x64/2016/08/20160826091944Logm.jpg",
							name:"香干肉丝"
						},
						{
							src:"http://mxu-img.dev.hoge.cn/material/auth/img/64x64/2016/08/20160826091944Logm.jpg",
							name:"水煮牛蛙"
						},
						{
							src:"http://mxu-img.dev.hoge.cn/material/auth/img/64x64/2016/08/20160826091944Logm.jpg",
							name:"尖椒炒蛋"
						}
					];
					vm.breakData=[
						{
							name:'包子',
							src:"http://mxu-img.dev.hoge.cn/material/auth/img/64x64/2016/08/20160826091944Logm.jpg",
							price:"1.5",
							num:1
						},
						{
							name:'馒头',
							src:"http://mxu-img.dev.hoge.cn/material/auth/img/64x64/2016/08/20160826091944Logm.jpg",
							price:"1",
							num:1
						},
						{
							name:'饺子',
							src:"http://mxu-img.dev.hoge.cn/material/auth/img/64x64/2016/08/20160826091944Logm.jpg",
							price:"4",
							num:1
						}
					];
				},

				addMenu:function(item){
					console.log(item);
				},

				reduce:function (item) {
					if(item.num>1){
						item.num-=1;
					}
				},
				plus:function(item){
					if(item.num>9){
						myUtils.confirm({
							msg:'你确定能吃的下?'
						},function(){
							item.num+=1;
						})
					}else{
						item.num+=1;
					}
				},
				
				getShortcut : function(){
					vm.shortcut_loading = true;
					http( config.getUrl('shortcut_operation'))
						.then( function( data ){
							if( data && ( data.error_code || data.error_message)){
								console.warn(data.error_message || '访问错误');
								vm.shortcut_loading = false;
								return false;
							}
							if( data && data.data ){
								var num = 0;
								vm.shortcutItem = data.data;
								angular.forEach(data.data,function(vv){
									if(vv.is_open == 1){
										num++;
									}
								});
								vm.shortcutItem.open_num = num;
								vm.shortcut_loading = false;
							}
						})
				},
      			
      			getLogs : function(){
      				/***获取动态信息***/
      				vm.log_loading = true;
      				vm.log_error = '';
      				var url = config.getUrl('logs_show')+'?count=6';
      				http( url)
      				.then( function( data ){
      					if( data && ( data.error_code || data.error_message)){
	      					vm.log_error = data.error_message || config.error_tip;
	      					vm.log_loading = false;
      						return false;
      					}
      					if( data && data.data ){
      						vm.log_loading = false;
							if(data.error_code == 0 && data.data.logs == []){
								vm.log_error = '暂无数据';
							}
      						if( $.isArray(data.data.logs) && data.data.logs[0]){
      							angular.forEach( data.data.logs,function( vv ){
	      							if( vv.operation && vv.operation.indexOf('删除') > -1 ){
	      								vv.del = true;
	      							}
	      							var curTime = myUtils.createTime();
	      							var time = curTime - parseInt(vv.create_time)*1000;
	      							time = myUtils.counter( time,'day:hour:min');
	      							time = time.split(':');
	      							if( time[0] && parseInt(time[0])){
	      								if( parseInt(time[0]) < 2){
	      									vv.time = parseInt(time[0])+'天前'
	      								}else{
	      									vv.time = vv.cre_time;
	      								}
	      							}else if( time[1] && parseInt(time[1]) ){
	      								vv.time = parseInt(time[1])+'小时前';
	      							}else if( time[2] && parseInt(time[2]) ){
	      								vv.time = parseInt(time[2])+'分钟前'
	      							}else{
	      								vv.time = '刚刚';
	      							}
	      							switch(vv.moudle_id){
	      								case '文稿' :{
	      									vv.mark  = 'wengao';
	      									vv.color  = '#68c986';
	      									break;
	      								}
	      								case '图集' :{
	      									vv.mark  = 'tuji';
	      									vv.color  = '#a988e1';
	      									break;
	      								}
	      								case '视频' :{
	      									vv.mark  = 'shipin';
	      									vv.color  = '#bca9a2';
	      									break;
	      								}
	      								case '外链' :{
	      									vv.mark  = 'wailian';
	      									vv.color  = '#8dc9fb';
	      									break;
	      								}
	      							}
									vv.img = myUtils.createImgsrc(vv.avatar, {width: 80, height: 80})
	      						});
	      						vm.logList = data.data.logs;
      						}
      					}
      				},function(){
      					vm.log_loading = false;
      				})
      			},

				shortCut:function () {
					var modalInstance = $uibModal.open({
						templateUrl: "../views/mxu/home/shortcut.html",
						controller: 'ctrl.myShortCut',
						windowClass:'shortCut',
						resolve: {
							param: function(){
								return vm.shortcutItem;
							}
						}
					});
					modalInstance.result.then(function ( result ) {
						if(result && result !== []){
							vm.shortcutItem = result;
						}
					})
				}
      		});
      		vm.init();
      	}

		function myShortCut($scope,$timeout,$uibModalInstance,param,config,http,myUtils){
			var vm = $scope.vm = {};
			angular.extend(vm,{
				init : function(){
					vm.List = [];
					angular.forEach(param,function(vv){
						vv.is_open = vv.is_open == 1? true : false;
					});
					vm.initItem = param;
					vm.List = angular.copy(param);
				},
				change: function(is_open,item){
					if(is_open){
						vm.initItem.open_num++;
					}else {
						vm.initItem.open_num--;
					}
					var url = config.getUrl('shortcut_operate') + '/' + item.id;
					http(url,'','put')
						.then(function(data){
							if(data.error_code == 0){

							}else{
								myUtils.tip({
									msg : data.error_message || '访问错误'
								});
								$uibModalInstance.close(vm.List);
								return false;
							}
						});
				},
				close : function(){
					$uibModalInstance.close('');
				}
			});
			vm.init();
		}
	
	    function menuNav() {
			var directive = {
				restrict: 'AC',
				link: function (scope, el, attr) {
					el.on('click','.i-b', function () {
						var li = angular.element(this).parent();
						li.toggleClass('active');
					});
				}
			};
			return directive;
		}
})();